﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

namespace A2Z.HttpMapping
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:UrlMapping runat=server></{0}:UrlMapping>")]
    public class UrlMapping : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        //public string Url
        //{
        //    get
        //    {
        //        String s = (String)ViewState["Url"];
        //        return ((s == null) ? String.Empty : s);
        //    }

        //    set
        //    {
        //        ViewState["Url"] = value;
        //    }
        //}


         

        public HtmlGenericControl CreateControl()
        {
            HtmlGenericControl gel = new HtmlGenericControl("div");
            Control ct;

            string page = GetPage();
            string action = GetAction();
            string pageName = page+action + ".ascx";
            string control = "Control/Mod_" + page+ "/"+pageName;
            try
            {
                ct = Page.LoadControl(control);

        
            }
            catch (HttpException ex)
            {
                control = "Control/Warning.ascx";
                ct = Page.LoadControl(control);
               
            }
            gel.Controls.Add(ct);
            return gel;


        }



        string GetPage()
        {
            object o = HttpContext.Current.Request.QueryString["page"];
            return (o == null) ? "Warning" : (string)o.ToString().Trim();
        }

        string GetAction()
        {
            object o = HttpContext.Current.Request.QueryString["action"];
            return (o == null) ? "" : (string)o.ToString();
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
           
        }
    }
}
